package com.example.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.file.service.FileStorageService;
import com.example.model.wemedia.pojo.Material;
import com.example.wemedia.mapper.MaterialMapper;
import com.example.wemedia.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public String uploadPicture(MultipartFile file) {
        try {
            // 获取原始文件名
            String originalFilename = file.getOriginalFilename();
            // 重新构造文件名称，防止重复，被覆盖
            String filePrefix = UUID.randomUUID().toString();
            // 获取文件后缀
            String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String url = fileStorageService.uploadImgFile("", filePrefix + postfix, file.getInputStream());
            return url;
        } catch (Exception e) {
            log.error("上传图片失败：{}", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}

